const Database = require('better-sqlite3');
const db = new Database('sqlite.db')


/* 获取合约信息
@param name :string 合约名称
@return :bool
*/

let getContract = async function(name){
    return new Promise(function(resolve, reject){
      let row = db.prepare('SELECT * from contract where name = ?').get(name)
      if (row) {
        resolve(row)
      }else{
        resolve(false)
      }
    })
  }



/* 将合约信息存储到数据中
@param name: string 合约名称
@param address :string 合约地址
@return :bool
*/
let saveContract = async function(name, address){
return new Promise(function(resolve, reject){
    try{
    let row = db.prepare('INSERT INTO contract (name, address) values (?, ?)')
    .run(name, address)
    resolve(true)
    }catch(e){
    console.log(e)
    resolve(false)
    }
})
}

module.exports = {
    getContract: getContract,
    saveContract: saveContract
}